$(function () {
    // 和后端交互 
    $('.regMobileBtn').click(function (e) {
        e.preventDefault()
        let mobile = $('.mobile').val()
        let code = $('.code').val()
        if (!mobile || !code) {
            cocoMessage.error("请填写表单数据", 3000);
            return
        }
        $.post(
            '/api/sms/registry.jsp',{ mobile, code },(res) => {
                if (res.meta.state === 201) {
                        cocoMessage.success("修改成功！", 3000);
                  
                    setTimeout(() => {
                        location.href = './login.html'
                    }, 3000)
                } else {
                    cocoMessage.error(res.meta.msg, 3000);
                }
            },'json'
        )
    })
    $('.captcha img').click(function(){
        $(this).attr('src',`http://kg.zhaodashen.cn/mt/web/captcha.jsp?t=`+(new Date).getTime())
    })
    //注册
    $('#zhuce').click(function(e){
    e.preventDefault()
    let uname=$('.account').val()
    let pwd=$('.pwd').val()
    let pwd2=$('.pwd2').val()
    let captcha=$('.captcha input').val()
    if(!uname||!pwd||!pwd2){
        cocoMessage.error('请输入账号密码', 3000);
        return
    }
    if(pwd!=pwd2){
        cocoMessage.error('密码不一致', 3000);
        return
    }
    $.post('/api/user/registry.jsp',{uname,pwd,captcha},res=>{
            // console.log(res);
            if(res.meta.state===201){
                cocoMessage.success("注册成功，请登录", 3000);
            setTimeout(function(){
                location.href='./login.html'
            },2500)
            }else{
                cocoMessage.error(res.meta.msg, 3000);
            }
        },'json')
    })
 // 切换注册方式
    $('.mobileBtn').click(function () {
        $('form').show()
        $(this).parents('form').hide()
    })
})
 // 行为验证码
$.ajax({
    url:'https://www.geetest.com/demo/gt/register-slide?t='+new Date().getTime(),type: 'get',dataType: 'json',
    success: function (data) {
        $('#text').hide()
        $('#wait').show()
        initGeetest(
            {
                gt: data.gt,
                challenge: data.challenge,
                offline: !data.success,
                new_captcha: data.new_captcha,
                product: 'popup',
                width: '280px',
                https: true,
            },
            function (captchaObj) {
                captchaObj.appendTo('#captcha')
                captchaObj.onReady(function () {
                    $('#wait').hide()
                })
                let shijian=10
                let t=null
                $('#yanzhengma').click(function(){
                    let mobile=$('.mobile').val()
                    if(mobile==''||!/^1\d{10}$/.test(mobile)){
                        cocoMessage.error('请输入正确的手机号', 3000);
                    return
                    }
                    // 判断行为验证码
                    let result = captchaObj.getValidate()
                    if (!result) {
                        cocoMessage.error('请先完成行为验证', 3000);
                        return
                    }
                    if(shijian!=10)return
                    shijian--
                    $(this).text(`重新发送验证码(${shijian})`)
                    t=setInterval(()=>{
                    if(shijian<1){
                        clearInterval(t)
                        shijian=10
                        $(this).text(`重新发送`)
                        return 
                    }else{
                        $(this).text(`重新发送验证码(${shijian})`)
                    }
                    shijian--
                    },1000)
                    $.post('/api/sms/send.jsp',{mobile},res=>{
                    // console.log(res);
                    if(res.meta.state!==201){
                        cocoMessage.error(res.meta.msg, 3000);
                    }
                    },'json')
                })
                window.gt = captchaObj
            }
        )
    },
})
